package com.example.demo.DateTest;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.nlf.calendar.Lunar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.util.*;

public class DateHutoolController {
    public static void main(String[] args) throws ParseException {
//        String monthBegin = DateUtil.getMonthBegin("2022-10-10");
//        System.out.println(monthBegin);
//
//        String yearBegin = DateUtil.getYearBegin("2022-10-10");
//        System.out.println(yearBegin);
//
//        String monthBeginToString = DateUtil.getMonthBeginToString(2022, 12);
//        System.out.println(monthBeginToString);
//
//        Date monthBeginToDate = DateUtil.getMonthBeginToDate(2022, 12);
//        System.out.println(monthBeginToDate);
//
//        String monthEnd = DateUtil.getMonthEnd("2022-10-01");
//        System.out.println(monthEnd);
        /** ----------------*/
        for (int i = 0; i < 10; i++) {
////         毫秒级
//            LocalDateTime now = LocalDateTime.now();
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
//            String formattedDateTime = now.format(formatter);
//            System.out.println(formattedDateTime);
//
//         微秒级
//            long nanos = System.nanoTime();
//            System.out.println(nanos);

////         微秒级
//            LocalDateTime now = LocalDateTime.now(); // 获取当前时间
//            int microSecond = now.getNano() / 1000; // 获取当前微秒数
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); // 定义日期时间格式
//            String formattedDateTime = now.format(formatter) + String.format("%06d", microSecond); // 格式化日期时间并加上微秒数
//            System.out.println(formattedDateTime);
        }

//        long l = System.currentTimeMillis();
//        Date date = new Date(l);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String formattedDate = sdf.format(date);
//        System.out.println(formattedDate);
//
//        long l1 = (Math.abs(l / 100000)) * 100000;
//        Date datel1 = new Date(l1);
//        SimpleDateFormat sdfl1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String formattedDatel1 = sdfl1.format(datel1);
//        System.out.println(formattedDatel1);
//
//        long l2 =  (Math.abs(l1 / 100000000)) * 100000000;
//        Date datel2 = new Date(l2);
//        SimpleDateFormat sdfl2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String formattedDatel2 = sdfl2.format(datel2);
//        System.out.println(formattedDatel2);
//
//        long l3 =  l2 - 100000000;
//        Date datel3 = new Date(l3);
//        SimpleDateFormat sdfl3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String formattedDatel3 = sdfl3.format(datel3);
//        System.out.println(formattedDatel3);

//
//        long a = System.currentTimeMillis();
//        a = (Math.abs(a / 100000)) * 100000;
//        for (int i = 0; i < 10; i++) {
//
//            Date aaDate = new Date(a);
//            SimpleDateFormat aaSdf = new SimpleDateFormat("yyyy-MM-dd");
//            String aaFormat = aaSdf.format(aaDate);
//
//            System.out.println(aaFormat);
//
//            a -= 100000000;
//
//        }

        /** ----------------*/
//        Date date = new Date();
//
//        for (int i = 0; i <= 4; i++) {
//            Date aaa = DateUtil.dateAdd(DateUtil.DateSort.DAY.getName(), -i, date);
//            String time = DateUtil.getDate(aaa);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            Date parse = sdf.parse(time);
//            long timestamp = parse.getTime();
//
//            System.out.println(timestamp);
//        }
        /** ----------------*/
//        Date newDate = new Date();
//        System.out.println(newDate);
//        Date date = DateUtil.toDate(newDate, "yyyy-MM-dd");
//        System.out.println(date);


        /** ----------------黄历---------------- */

//        JSONObject chinaCalendar = getChinaCalendar("2025-01-03");
//        Set<Map.Entry<String, Object>> entries = chinaCalendar.entrySet();
//        for (Map.Entry<String, Object> entry : entries) {
//            System.out.println(entry);
//        }

//        String endDate = cn.hutool.core.date.DateUtil.today();
//        String startDate = cn.hutool.core.date.DateUtil.offset(DateUtil.date(), DateField.MONTH, -3).toString("yyyy-MM-01");
//        System.out.println(endDate);
//        System.out.println(startDate);

        /** ----------------*/
//
//        String workStartTime = "2025-05-14 08:20:30";
//        String workEndTime = "2025-05-14 19:32:16";
//
//        // 解析字符串为日期对象
//        long betweenSeconds = DateUtil.between(
//                DateUtil.parse(workStartTime),
//                DateUtil.parse(workEndTime),
//                DateUnit.SECOND
//        );
//
//        System.out.println("时间差(秒): " + betweenSeconds);

        /** ----------------*/
//        LinkedList<String> dayStartAndEndTime = getDayStartAndEndTime(-1);
//        System.out.println(dayStartAndEndTime.get(0));
//        System.out.println(dayStartAndEndTime.get(1));

        /** ----------------*/
        String startDate = "2025-10-22";
        String endDate = "2026-02-02";
        datePrinting(startDate, endDate);

    }

    private static void datePrinting(String startDateStr, String endDateStr) {
        Map<String, String> HOLIDAYS = new HashMap<>();
        Map<String, String> WORKDAYS = new HashMap<>();
        // 2025年部分节假日示例（实际需根据当年官方通知更新）
        // 国庆节
        HOLIDAYS.put("2025-10-01", "国庆");
        HOLIDAYS.put("2025-10-02", "国庆");
        HOLIDAYS.put("2025-10-03", "国庆");
        HOLIDAYS.put("2025-10-04", "国庆");
        HOLIDAYS.put("2025-10-05", "国庆");
        HOLIDAYS.put("2025-10-06", "国庆");
        HOLIDAYS.put("2025-10-07", "国庆");
        // 冬至
        HOLIDAYS.put("2025-12-21", "冬至");
        // 圣诞
        HOLIDAYS.put("2025-12-25", "圣诞");
        // 元旦
        HOLIDAYS.put("2026-01-01", "元旦");

        // 调休工作日示例（示例：国庆调休）
        WORKDAYS.put("2025-10-11", "调休");
        WORKDAYS.put("2025-10-12", "调休");

        DateTime startDate = DateUtil.parse(startDateStr);
        DateTime endDate = DateUtil.parse(endDateStr);

        // Hutool星期映射：1=周日，2=周一...7=周六
        String[] weekTexts = {"日", "一", "二", "三", "四", "五", "六"};

        // 计算补齐到周一的偏移量
        int dayOfWeek = startDate.dayOfWeek();
        int offset = (dayOfWeek == 1) ? -6 : (2 - dayOfWeek);
        DateTime currentDate = DateUtil.offsetDay(startDate, offset);

        int weekSum = 1;

        while (currentDate.isBefore(endDate) || currentDate.equals(endDate)) {
            String dateStr = DateUtil.format(currentDate, "yyyy-MM-dd");
            int currentDayOfWeek = currentDate.dayOfWeek();
            String weekStr = weekTexts[currentDayOfWeek - 1];

            // 构建输出字符串（包含节假日/调休标记）
            StringBuilder output = new StringBuilder();
            output.append(dateStr).append("(周").append(weekStr).append(")");

            // 标记节假日
            if (HOLIDAYS.containsKey(dateStr)) {
                output.append("[").append(HOLIDAYS.get(dateStr)).append("]");
            }
            // 标记调休工作日
            else if (WORKDAYS.containsKey(dateStr)) {
                output.append("[调休]");
            }
            // 普通周末（非调休也非节假日）
            else if (currentDayOfWeek == 1 || currentDayOfWeek == 7) { // 周日或周六
                output.append("[休]");
            } else {
                output.append("[XXX]");
            }

            // 周日末尾添加周数标记并换行
            if (currentDayOfWeek == 1) { // 周日（Hutool中1=周日）
                output.append(" 第").append(weekSum).append("周");
                switch (weekSum){
                    case 3:
                        output.append("🌟");
                        break;
                    case 7:
                        output.append("🌟");
                        break;
                    case 11:
                        output.append("🌟");
                        break;
                    default:
                        break;
                }
                System.out.println(output);
                weekSum++; // 下周计数
            } else {
                System.out.print(output.append(" ").toString());
            }


            currentDate = DateUtil.offsetDay(currentDate, 1);
        }
    }


//    public static LinkedList<String> getDayStartAndEndTime(int day) {
//        // 1. 计算目标日期（当前日期 + 偏移天数）
//        DateTime targetDate = DateUtil.offsetDay(new Date(), day);
//
//        // 2. 获取当天的开始时间（00:00:00）
//        DateTime startTime = DateUtil.beginOfDay(targetDate);
//
//        // 3. 获取当天的结束时间（23:59:59）
//        DateTime endTime = DateUtil.endOfDay(targetDate);
//
//        // 4. 格式化为字符串
//        String pattern = "yyyy-MM-dd HH:mm:ss";
//        String startTimeStr = DateUtil.format(startTime, pattern);
//        String endTimeStr = DateUtil.format(endTime, pattern);
//
//        // 5. 存入 LinkedList 并返回
//        LinkedList<String> dateList = new LinkedList<>();
//        dateList.add(startTimeStr);
//        dateList.add(endTimeStr);
//        return dateList;
//    }

    /**
     * gl:公历/阳历
     * nj:农历/阴历
     * xs:喜神方位
     * ygs1:阳贵神方位
     * ygs2:阴贵神方位
     * fs:福神方位
     * cs1:财神方位
     * cs2：冲煞
     * zx:十二值星
     * ts:十二天神
     * yearGz: 农历年
     * monthGz: 农历月
     * dayGz: 农历日
     * sx: 生肖
     * week: 星期
     * nl: 农历日期
     * y:宜
     * j:忌
     * jsyq:吉神宜趋
     * xsyj:凶煞宜忌
     */
    public static JSONObject getChinaCalendar(String targetDate) throws ParseException {
        JSONObject obj = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(targetDate); // 将日期字符串解析为 Date 对象
        //今天
        Lunar lunar = new Lunar(date);
        obj.set("gl", lunar.getSolar().toFullString());
        obj.set("nlYear", lunar.getYearInChinese() + "年 ");
        obj.set("nlMonth", lunar.getMonthInChinese() + "月 ");
        obj.set("nlDay", lunar.getDayInChinese() + "日 ");
        obj.set("nlxq", "星期" + lunar.getWeekInChinese());
        obj.set("xs", lunar.getDayPositionXi() + " = " + lunar.getDayPositionXiDesc());
        obj.set("ygs1", lunar.getDayPositionYangGui() + " = " + lunar.getDayPositionYangGuiDesc());
        obj.set("ygs2", lunar.getDayPositionYinGui() + " = " + lunar.getDayPositionYinGuiDesc());
        obj.set("fs", lunar.getDayPositionFu() + " = " + lunar.getDayPositionFuDesc());
        obj.set("cs1", lunar.getDayPositionCai() + " = " + lunar.getDayPositionCaiDesc());
        obj.set("cs2", "冲：" + lunar.getDayChongDesc() + " 煞：" + lunar.getDaySha());
        obj.set("zx", lunar.getZhiXing());
        obj.set("ts", lunar.getDayTianShen() + " 天神道：" + lunar.getDayTianShenType() + " 天神吉凶：" + lunar.getDayTianShenLuck());

        obj.set("yearGz", lunar.getYearInGanZhi());
        obj.set("monthGz", lunar.getMonthInGanZhiExact());
        obj.set("dayGz", lunar.getDayInGanZhi());
        obj.set("sx", lunar.getYearShengXiao());
        obj.set("week", lunar.getWeekInChinese());

        obj.set("nl", lunar.getMonthInChinese() + "月" + lunar.getDayInChinese());

        StringBuilder y = new StringBuilder();
        List<String> l = lunar.getDayYi();
        for (String s : l) {
            y.append(s).append(" ");
        }
        obj.set("y", y);

        StringBuilder j = new StringBuilder();
        l = lunar.getDayJi();
        for (String s : l) {
            j.append(s).append(" ");
        }
        obj.set("j", j);

        StringBuilder js = new StringBuilder();
        List<String> ll = lunar.getDayJiShen();
        for (String s : ll) {
            js.append(s).append(" ");
        }
        obj.set("jsyq", js);

        StringBuilder xs = new StringBuilder();
        ll = lunar.getDayXiongSha();
        for (String s : ll) {
            xs.append(s).append(" ");
        }
        obj.set("xsyj", xs);
        return obj;
    }

}
